<template>
  <div class="app">
    <a-breadcrumb style="margin-bottom: 10px">
      <a-breadcrumb-item>校园及周边安全</a-breadcrumb-item>
    </a-breadcrumb>
    <a-row class="pd20 mt20">
      <a-col :span="6">
        学校名称：
        <a-input placeholder="请输入" v-model="queryForm.schoolName" />
      </a-col>
      <a-col :span="6">
        校长姓名：
        <a-input placeholder="请输入" v-model="queryForm.headMaster" />
      </a-col>
      <a-col :span="6">
        校长联系电话：
        <a-input placeholder="请输入" v-model="queryForm.headMasterPhone" />
      </a-col>
      <!-- <a-col :span="6">
        检查结果：
        <a-select placeholder="请选择"
                  v-model="queryForm.checkResult">
          <a-select-option v-for="(item,index) in resultList"
                           :key="index"
                           :value="item.value">
            {{ item.label }}
          </a-select-option>
        </a-select>
      </a-col> -->
      <a-col :span="6">
        安全负责人：
        <a-input placeholder="请输入" v-model="queryForm.securityPerson" />
      </a-col>
      <a-col :span="6">
        安全负责人联系电话：
        <a-input placeholder="请输入" v-model="queryForm.securityPersonPhone" />
      </a-col>
      <a-col :span="8">
        创建时间范围：
        <a-range-picker
          style="width: 330px; margin-left: 10px"
          v-model="dateRange"
          :placeholder="['开始日期', '结束日期']"
          valueFormat="YYYY-MM-DD HH:mm:ss"
          :show-time="{
            hideDisabledOptions: true,
            defaultValue: [
              moment('00:00:00', 'HH:mm:ss'),
              moment('23:59:59', 'HH:mm:ss'),
            ],
          }"
          format="YYYY-MM-DD HH:mm:ss"
          @change="onChange"
        />
      </a-col>
      <a-col :span="4">
        <a-button type="primary" @click="getList"> 查询 </a-button>&emsp;&nbsp;
        <a-button @click="cleanQuery">重置</a-button>
      </a-col>
    </a-row>
    <a-row class="pd20 mt20">
      <div class="btn_box">
        <div>校园及周边安全</div>
        <a-button
          type="primary"
          @click="addFn"
          v-hasPermi="['zhonghe:xyzbjaq:add']"
          >添加</a-button
        >
      </div>
      <a-spin :spinning="loading">
        <a-table
          :scroll="{ x: 500 }"
          :columns="columns"
          :data-source="tableData"
          :pagination="false"
        >
          <template slot="description" slot-scope="text, record">
            <ToolTip :text="text" />
          </template>
          <template slot="headMasterPhone" slot-scope="text, record">
            <div class="phone-cell">
              <span>{{
                record.showPhone ? text : $desensitizePhone(text)
              }}</span>
              <a-icon
                :type="record.showPhone ? 'eye-invisible' : 'eye'"
                @click.stop="togglePhoneVisibility1(record)"
              />
            </div>
          </template>
          <template slot="securityPersonPhone" slot-scope="text, record">
            <div class="phone-cell">
              <span>{{
                record.showPhone2 ? text : $desensitizePhone(text)
              }}</span>
              <a-icon
                :type="record.showPhone ? 'eye-invisible' : 'eye'"
                @click.stop="togglePhoneVisibility2(record)"
              />
            </div>
          </template>
          <span slot="customTitle"><a-icon type="smile-o" /> Name</span>
          <span slot="action" slot-scope="text, record">
            <a
              @click="editFn(text, 'view')"
              v-hasPermi="['zhonghe:zdrqlxgl:edit']"
              >查看</a
            >
            <a-divider type="vertical" />
            <a
              @click="editFn(text, 'edit')"
              v-hasPermi="['zhonghe:zdrqxx:edit']"
              >修改</a
            >
            <a-divider type="vertical" />
            <a
              @click="delFn(text, record)"
              v-hasPermi="['zhonghe:xyzbjaq:del']"
              class="red"
              >删除</a
            >
          </span>
        </a-table>
        <div class="page_box">
          <a-pagination
            v-model="queryForm.offset"
            :page-size-options="['20', '30', '40', '50']"
            :total="total"
            show-size-changer
            :page-size="queryForm.current"
            @showSizeChange="onShowSizeChange"
            @change="pageChange"
            :show-total="(total) => `共 ${total} 条`"
          ></a-pagination>
        </div>
      </a-spin>
    </a-row>
    <a-modal
      title="校园信息配置"
      :visible="dialogVisible"
      width="38%"
      ok-text="确认"
      cancel-text="取消"
      @cancel="dialogVisible = false"
      @ok="putDict"
    >
      <a-form :form="form" style="height: 550px; overflow-y: auto">
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="学校名称"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'schoolName',
              { rules: [{ required: true, message: '请输入学校名称' }] },
            ]"
            placeholder="请输入学校名称"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="学校地址"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'schoolAddr',
              { rules: [{ required: true, message: '请输入学校地址' }] },
            ]"
            placeholder="请输入学校地址"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="校长姓名"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'headMaster',
              { rules: [{ required: true, message: '请输入校长姓名' }] },
            ]"
            placeholder="请输入校长姓名"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="字典校长联系电话"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'headMasterPhone',
              { rules: [{ required: true, message: '请输入校长联系电话' }] },
            ]"
            placeholder="请输入校长联系电话"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="安全负责人"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'securityPerson',
              { rules: [{ required: true, message: '请输入安全负责人' }] },
            ]"
            placeholder="请输入安全负责人"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="安全负责人联系电话"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'securityPersonPhone',
              {
                rules: [
                  { required: true, message: '请输入字安全负责人联系电话' },
                ],
              },
            ]"
            placeholder="请输入安全负责人联系电话"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="检查日期"
        >
          <a-date-picker
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'checkTime',
              { rules: [{ required: true, message: '请输入检查日期' }] },
            ]"
            placeholder="请输入检查日期"
            valueFormat="YYYY-MM-DD"
            format="YYYY-MM-DD"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="检查结果"
        >
          <a-select
            :disabled="ModalType == 'view'"
            style="width: 100%"
            v-decorator="[
              'checkResult',
              { rules: [{ required: true, message: '请选择检查结果' }] },
            ]"
            placeholder="请选择检查结果"
          >
            <a-select-option
              v-for="(item, index) in resultList"
              :key="index"
              :value="item.value"
            >
              {{ item.label }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="安全隐患"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'securityBug',
              { rules: [{ required: true, message: '请输入安全隐患' }] },
            ]"
            placeholder="请输入安全隐患"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="已采取的措施"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'fiexWay',
              { rules: [{ required: true, message: '请输入已采取的措施' }] },
            ]"
            placeholder="请输入已采取的措施"
          />
        </a-form-item>
        <a-form-item
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
          label="备注"
        >
          <a-input
            style="width: 100%"
            :disabled="ModalType == 'view'"
            v-decorator="[
              'descStr',
              { rules: [{ required: true, message: '请输入备注' }] },
            ]"
            placeholder="请输入备注"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { Modal } from "ant-design-vue";
import { message } from "ant-design-vue";
import dayjs from "dayjs";
import moment from "moment";
export default {
  data() {
    return {
      dateRange: [],
      loading: true,
      dialogVisible: false,
      queryForm: { current: 20, offset: 1 },
      total: 0,
      form: this.$form.createForm(this, { name: "coordinated" }),
      id: null,
      tableData: [],
      columns: [
        {
          title: "学校ID",
          dataIndex: "id",
          key: "id",
          width: 150,
        },
        {
          title: "学校名称",
          dataIndex: "schoolName",
          key: "schoolName",
          width: 150,
        },
        {
          title: "学校地址",
          dataIndex: "schoolAddr",
          key: "schoolAddr",
          width: 150,
        },
        {
          title: "校长姓名",
          dataIndex: "headMaster",
          key: "headMaster",
          width: 150,
        },
        {
          title: "校长联系电话",
          dataIndex: "headMasterPhone",
          key: "headMasterPhone",
          scopedSlots: { customRender: "headMasterPhone" },
          width: 150,
        },
        {
          title: "安全负责人",
          dataIndex: "securityPerson",
          key: "securityPerson",
          width: 150,
        },
        {
          title: "安全负责人联系电话",
          dataIndex: "securityPersonPhone",
          key: "securityPersonPhone",
          scopedSlots: { customRender: "securityPersonPhone" },
          width: 150,
        },
        {
          title: "检查日期",
          dataIndex: "checkTime",
          key: "checkTime",
          width: 150,
        },
        {
          title: "检查结果",
          dataIndex: "checkResultName",
          key: "checkResultName",
          width: 150,
        },
        {
          title: "安全隐患",
          dataIndex: "securityBug",
          key: "securityBug",
          scopedSlots: { customRender: "description" },
          width: 150,
        },
        {
          title: "已采取的措施",
          dataIndex: "fiexWay",
          key: "fiexWay",
          scopedSlots: { customRender: "description" },
          width: 150,
        },
        {
          title: "备注",
          dataIndex: "descStr",
          key: "descStr",
          scopedSlots: { customRender: "description" },
          width: 150,
        },
        {
          title: "最近更新时间",
          dataIndex: "updateTime",
          width: 150,
          key: "updateTime",
        },
        {
          title: "创建时间",
          dataIndex: "createTime",
          key: "createTime",
          width: 150,
        },
        {
          title: "操作",
          key: "action",
          width: 150,
          scopedSlots: { customRender: "action" },
        },
      ],
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 18 },
      },
      formTailLayout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 19, offset: 4 },
      },
      resultList: [
        {
          value: "1",
          label: "良好",
        },
        {
          value: "2",
          label: "优秀",
        },
      ],
      ModalType: "",
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    moment,
    getList() {
      this.loading = true;
      const url = "/government-platform-center/schoolSecurity";
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.tableData = res.data.data.map((item) => {
              item["showPhone"] = false;
              item["showPhone2"] = false;
              if (item.checkResult == 1) {
                item["checkResultName"] = "良好";
              } else {
                item["checkResultName"] = "良好";
              }
              return item;
            });
            this.total = res.data.total;
            this.loading = false;
          }
        });
    },
    cleanQuery() {
      this.queryForm = { current: 20, offset: 1 };
      this.dateRange = [];
      this.getList();
    },
    // 分页
    onShowSizeChange(current, pageSize) {
      this.queryForm.current = pageSize;
      this.getList();
    },
    pageChange(current) {
      this.queryForm.offset = current;
      this.getList();
    },
    delFn(text, record) {
      const that = this;
      Modal.confirm({
        title: "确认删除",
        content: "您确定要删除这项内容吗？",
        okText: "确认",
        cancelText: "取消",
        onOk() {
          const url = `/government-platform-center/schoolSecurity/deleteById`;
          const form = { id: text.id };
          that.$axios
            .post(url, form, {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.code == 0) {
                that.getList();
                message.success("操作成功");
              }
            });
        },
        onCancel() {
          // 在此处理取消逻辑
          console.log("删除已取消");
        },
      });
    },
    goData(text, record) {
      this.$router.push({
        path: "/dataDict",
        query: { dictType: text.dictType, id: text.id },
      });
    },
    addFn() {
      this.dialogVisible = true;
      this.id = null;
      this.ModalType = "add";
      this.form.resetFields();
    },
    editFn(text, type) {
      this.ModalType = type;
      this.dialogVisible = true;
      const url = `/government-platform-center/schoolSecurity/${text.id}`;
      const params = this.queryForm;
      this.$axios
        .get(url, {
          params: params,
        })
        .then((res) => {
          if (res.data.code == 0) {
            this.form.setFieldsValue(res.data.data);
            this.id = res.data.data.id;
          }
        });
    },
    putDict() {
      this.form.validateFields((err, values) => {
        if (err) {
          return;
        }
        if (!this.id) {
          const form = values;
          form.checkTime = dayjs(form.checkTime).format("YYYY-MM-DD");
          console.log(form, "form");
          const url = "/government-platform-center/schoolSecurity";
          this.$axios
            .post(url, form, {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.code == 0) {
                this.getList();
                this.reSetForm();
                this.dialogVisible = false;
                message.success("操作成功");
              }
            });
        } else {
          const form = { ...values, id: this.id };
          const url = "/government-platform-center/schoolSecurity/edit";
          this.$axios
            .post(url, form, {
              headers: {
                "Content-Type": "application/x-www-form-urlencoded",
              },
            })
            .then((res) => {
              if (res.data.code == 0) {
                this.getList();
                this.reSetForm();
                this.dialogVisible = false;
                message.success("操作成功");
              }
            });
        }
      });
    },
    reSetForm() {
      // this.form = { dictName: null, dictType: null, remark: null }
    },
    onChange(dateStrings) {
      this.queryForm.startTime = dateStrings[0];
      this.queryForm.endTime = dateStrings[1];
    },
    togglePhoneVisibility1(record) {
      this.$set(record, "showPhone", !record.showPhone);
    },
    togglePhoneVisibility2(record) {
      this.$set(record, "showPhone2", !record.showPhone2);
    },
  },
};
</script>

<style lang="scss" scoped>
.ant-row {
  border-radius: 5px;

  background: #fff;
  color: #000;
}

v-deep {
  .ant-table-thead > tr > th {
    color: #000;
    font-weight: bold;
  }
  .ant-table-tbody > tr > td {
    color: #000;
  }
  .el-table thead {
    color: black;
    font-weight: bold;
    background: #fafafa;
  }
  .el-table th.el-table__cell {
    background: #fafafa;
  }
}

.ant-col {
  margin: 10px 0;
}

.ant-select {
  width: 60%;
}

.ant-input {
  width: 60%;
}
/* 添加表格原生滚动条样式 */
::v-deep .ant-table-body {
  overflow-x: auto !important;
  /* overflow-y: auto !important; */
  /* max-height: 500px; 设置最大高度，超出时显示垂直滚动条 */
}

/* 确保使用原生滚动条样式 */
::v-deep .ant-table-body::-webkit-scrollbar {
  width: 8px !important;
  height: 8px !important;
}

::v-deep .ant-table-body::-webkit-scrollbar-thumb {
  background-color: #c1c1c1 !important;
  border-radius: 4px !important;
}

::v-deep .ant-table-body::-webkit-scrollbar-track {
  background-color: #f1f1f1 !important;
}

/* 修复固定列的问题 */
::v-deep .ant-table-fixed-right {
  right: 8px !important; /* 为滚动条留出空间 */
}
</style>
